package org.spring.springboot.scheduled;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.domain.City;
import org.spring.springboot.redis.service.RedisService;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledService {
	
	private static String CACHE_LOCK = "cache_lock";
	 
    private static int EXPIRE_PERIOD = (int)DateUtils.MILLIS_PER_HOUR / 1000;
    private static int TIMEOUT = 250;
    
    private  Logger logger = LoggerFactory.getLogger(ScheduledService.class);
    
    @Autowired
    private RedisService redisService;
    
    @Autowired
    private CityService cityService;
 
    @Scheduled(cron = "0 0,5,10,15,20,25,30,35,40,45,50 * * * *")
    public void build() {
        if (redisService.get(CACHE_LOCK) == null) {
        	redisService.set(CACHE_LOCK, true, TIMEOUT);
            doJob();
        }
    }

	private void doJob() {
		ExecutorService service=Executors.newFixedThreadPool(5);   //创建一个固定数量（5）的线程池
		City city = new City();
		
		service.submit(new TaskThread(city));       //线程池Random  那出一个线程供使用
		service.shutdown();
		logger.info("zhixingdizhi1--------------------22222222222222222-");
	}


	class TaskThread extends Thread{
		
		private City city;
		
		public City getCity() {
			return city;
		}
		public void setCity(City city) {
			this.city = city;
		}
		
		public TaskThread(City city) {
			super();
			this.city = city;
		}
		
		@Override
		public void run() {
			
			try {
				redisService.set("City_key", city);
				
				Thread.sleep(1000);
				City city = cityService.findCityById(11L);
				System.out.println("---------------" + city.getCityName());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
